package me.zk.practice1.s;

import java.util.ArrayDeque;
import java.util.LinkedList;
import java.util.Queue;

public class LS101 {

    public boolean isSymmetric(TreeNode root) {

        return get(root.left,root.right);

    }

    public boolean get(TreeNode l,TreeNode r) {
        if (l == null && r == null) {
            return true;
        }
        if (l==null || r ==null) {
            return false;
        }
        if (l.val != r.val) {
            return false;
        }

        return get(l.left,r.right) && get(l.right,r.left);


    }

    public boolean isSymmetric2(TreeNode root) {

        LinkedList<TreeNode> queue = new LinkedList<>();
        if (root == null || (root.left == null && root.right == null)) {
            return true;
        }
        queue.offer(root.left);
        queue.offer(root.right);
        while (!queue.isEmpty()) {
            //取出
            TreeNode l = queue.poll();
            TreeNode r = queue.poll();
            //判断两个节点
            if (l == null && r == null) {
                continue;
            }
            if (l==null || r == null) {
                return false;
            }
            if  (l.val != r.val ){
                return false;
            }
            queue.offer(l.left);
            queue.offer(r.right);
            queue.offer(l.right);
            queue.offer(r.left);

        }
        return true;

    }



}
